package structures;

import java.io.BufferedReader;




import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.net.ssl.HttpsURLConnection;

import twitter4j.internal.org.json.JSONException;
import twitter4j.internal.org.json.JSONObject;
import twitter4j.internal.org.json.JSONArray;
import managers.HashManager;

public class GamifyUser {

	private String secretKey = "24dcb7b6462fc278dbd7473750cf2bcc";
	private String schemaName = "servicetechnologies";
	private final String USER_AGENT = "Mozilla/5.0";
	HashManager hm = new HashManager();

 
		public static void main(String[] args) throws Exception {
 
		GamifyUser http = new GamifyUser();
 
		System.out.println("Testing 1 - Send Http GET request");
	//	http.obtainToken();
	//	http.obtainBadges();
	//  http.getUserBadges("1");
	//	http.obtainActions();
	//	http.obtainActionGroups();

 
		System.out.println("\nTesting 2 - Send Http POST request");
	//  http.addAction();
		 http.addBadge();
	//  http.addActionGroup();
	//  http.allowIpAddress();

		}
		
		// authorizes an ip to access the specified schema
		public void allowIpAddress() throws Exception{
			
			
			System.setProperty("jsse.enableSNIExtension", "false");
			String ip = InetAddress.getLocalHost().getHostAddress();
			String token = hm.obtainToken(schemaName,secretKey);
			
		
				String urlParameters = "ns="+schemaName+"&token="+token+"&ip=0.0.0.0/8&add_or_remove=remove";
				String url = "https://www.gamify.ws/api/v1/schemeips";
				
				URL obj = new URL(url);
				HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
		 		
				
				//add request header
				con.setRequestMethod("POST");
			
				con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
				con.setRequestProperty("charset", "utf-8");
				
				// Send post request
				con.setDoOutput(true);
				con.connect();
				DataOutputStream wr = new DataOutputStream(con.getOutputStream());
				wr.writeBytes(urlParameters);
				wr.flush();
				wr.close();
				
				BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
				String inputLine;
				StringBuffer responseBuffer = new StringBuffer();
		 
				while ((inputLine = in.readLine()) != null) {
					responseBuffer.append(inputLine);
				}
				in.close();
				
				String response = responseBuffer.toString();
				//print result
				System.out.println(response.toString());	
				}
		
		// returns the url of a badge
		public void getBadgeUrl(String badge_name) throws IOException, JSONException{
			
			System.setProperty("jsse.enableSNIExtension", "false");
			String url = "https://www.gamify.ws/api/v1/badges/"+schemaName+"?id="+badge_name;
			JSONObject answerJson;
			
			URL obj = new URL(url);
			
			
			HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

			con.setRequestMethod("GET");
		 
			//add request header
			con.setRequestProperty("User-Agent", USER_AGENT);

		 
				int responseCode1 = con.getResponseCode();

				System.out.println("\nSending 'GET' request to URL : " + url);

				System.out.println("Response Code : " + responseCode1); 
				
		 
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));


			String inputLine;
			StringBuffer responseBuffer = new StringBuffer();
				
		 
			while ((inputLine = in.readLine()) != null) {
				responseBuffer.append(inputLine);
				}
				
				in.close();	
		 
			String response = responseBuffer.toString();
							
			// obtaining the badges' ids 
			answerJson = new JSONObject(response);
	 			
			String badge_url= new String();
			JSONArray badgeArray = answerJson.getJSONArray("data");
			
			if(!badgeArray.isNull(0)){
				badge_url=(badgeArray.getJSONObject(0).getString("badge_image_name"));
				}
			
			System.out.println(badge_url);
			} 
	
		//add a gamify action to the specified scheme
		private void addAction() throws Exception {
			
		System.setProperty("jsse.enableSNIExtension", "false");
	
			String token = hm.obtainToken(schemaName,secretKey);
			String urlParameters = "ns=servicetechnologies&token="+token+"&action_name=PositiveFeedback";
			String url = "https://www.gamify.ws/api/v1/actions";
 
			URL obj = new URL(url);
			HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
	 		
			
			//add request header
			con.setRequestMethod("POST");
		
			con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
			con.setRequestProperty("charset", "utf-8");
			
			// Send post request
			con.setDoOutput(true);
			con.connect();
			DataOutputStream wr = new DataOutputStream(con.getOutputStream());
			wr.writeBytes(urlParameters);
			wr.flush();
			wr.close();
	 
			int responseCode = con.getResponseCode();
			System.out.println("\nSending 'POST' request to URL : " + url);
			System.out.println("Post parameters : " + urlParameters);
			System.out.println("Response Code : " + responseCode);
	 
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuffer response = new StringBuffer();
	 
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();
			
	 
			//print result
			System.out.println(response.toString());				
			} 
		
		//add a badge to the gamify schema
		private void addBadge() throws Exception {
		
		System.setProperty("jsse.enableSNIExtension", "false");
	
			String token = hm.obtainToken(schemaName,secretKey);
			String urlParameters = "ns=servicetechnologies&token="+token+"&badge_name=ThreePositiveFeedback&url=http://lordhowekitesurf.com/wp-content/uploads/2013/08/3star.png&badge_type=points&badge_value=3";
			String url = "https://www.gamify.ws/api/v1/badges";
 
			URL obj = new URL(url);
			HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
	 		
			
			//add request header
			con.setRequestMethod("POST");
		
			con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
			con.setRequestProperty("charset", "utf-8");
			
			// Send post request
			con.setDoOutput(true);
			con.connect();
			DataOutputStream wr = new DataOutputStream(con.getOutputStream());
			wr.writeBytes(urlParameters);
			wr.flush();
			wr.close();
	 
			int responseCode = con.getResponseCode();
			System.out.println("\nSending 'POST' request to URL : " + url);
			System.out.println("Post parameters : " + urlParameters);
			System.out.println("Response Code : " + responseCode);
	 
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuffer response = new StringBuffer();
	 
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();
			
	 
			//print result
			System.out.println(response.toString());				
			} 
	
		// get the actions of the gamify schema
		private void obtainActions() throws Exception {
		
		System.setProperty("jsse.enableSNIExtension", "false");

		JSONObject answerJson;
		String url = "https://www.gamify.ws/api/v1/actions/"+schemaName+"";
  	 
		URL obj = new URL(url);
		
		HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

		// optional default is GET
		con.setRequestMethod("GET");
 
		//add request header
		con.setRequestProperty("User-Agent", USER_AGENT);

 
		int responseCode1 = con.getResponseCode();

		System.out.println("\nSending 'GET' request to URL : " + url);

		System.out.println("Response Code : " + responseCode1);
		
 
		BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));


		String inputLine;
		StringBuffer responseBuffer = new StringBuffer();
		
 
		while ((inputLine = in.readLine()) != null) {
			responseBuffer.append(inputLine);
		}
		
		in.close();	
 
		String response = responseBuffer.toString();
		//print result
		System.out.println(response.toString());
		}
	
		//get the schema's available badges
		private void obtainBadges() throws Exception {
		
		System.setProperty("jsse.enableSNIExtension", "false");

		String url = "https://www.gamify.ws/api/v1/badges/"+schemaName+"";
  	 
		URL obj = new URL(url);
		
		HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

		// optional default is GET
		con.setRequestMethod("GET");
 
		//add request header
		con.setRequestProperty("User-Agent", USER_AGENT);

 
		int responseCode1 = con.getResponseCode();

		System.out.println("\nSending 'GET' request to URL : " + url);

		System.out.println("Response Code : " + responseCode1);
		
 
		BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));


		String inputLine;
		StringBuffer responseBuffer = new StringBuffer();
		
 
		while ((inputLine = in.readLine()) != null) {
			responseBuffer.append(inputLine);
		}
		
		in.close();	
 
		String response = responseBuffer.toString();
		//print result
		System.out.println(response.toString());		
		}
	
		// get the action groups of the schema
		private void obtainActionGroups() throws Exception {
		
		System.setProperty("jsse.enableSNIExtension", "false");

		JSONObject answerJson;
		String url = "https://www.gamify.ws/api/v1/action_groups/"+schemaName+"";
  	 
		URL obj = new URL(url);
		
		HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

		// optional default is GET
		con.setRequestMethod("GET");
 
		//add request header
		con.setRequestProperty("User-Agent", USER_AGENT);

 
		int responseCode1 = con.getResponseCode();

		System.out.println("\nSending 'GET' request to URL : " + url);

		System.out.println("Response Code : " + responseCode1);
		
 
		BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));


		String inputLine;
		StringBuffer responseBuffer = new StringBuffer();
		
 
		while ((inputLine = in.readLine()) != null) {
			responseBuffer.append(inputLine);
		}
		
		in.close();	
 
		String response = responseBuffer.toString();
		//print result
		System.out.println(response.toString());
		}
		
		//add an action group to the gamify schema
		private void addActionGroup() throws Exception {
		
		System.setProperty("jsse.enableSNIExtension", "false");
	
			String token = hm.obtainToken(schemaName,secretKey);
			String urlParameters = "ns=servicetechnologies&token="+token+"&action_group_name=userGetsOnePositiveFeedback&actions=A,PositiveFeedback,1&points_multiplier=1";
			String url = "https://www.gamify.ws/api/v1/action_groups";
 
			URL obj = new URL(url);
			HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
	 		
			
			//add request header
			con.setRequestMethod("POST");
		
			con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
			con.setRequestProperty("charset", "utf-8");
			
			// Send post request
			con.setDoOutput(true);
			con.connect();
			DataOutputStream wr = new DataOutputStream(con.getOutputStream());
			wr.writeBytes(urlParameters);
			wr.flush();
			wr.close();
	 
			int responseCode = con.getResponseCode();
			System.out.println("\nSending 'POST' request to URL : " + url);
			System.out.println("Post parameters : " + urlParameters);
			System.out.println("Response Code : " + responseCode);
	 
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuffer response = new StringBuffer();
	 
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();
				 
			//print result
			System.out.println(response.toString());				
			} 
	
		//add a gamify user's event
		private void addEvent() throws Exception {
		
		System.setProperty("jsse.enableSNIExtension", "false");
	
			String token = hm.obtainToken(schemaName,secretKey);
			String urlParameters = "ns="+schemaName+"&token="+token+"&action=5PositiveFeedback&user_id=1";
			String url = "https://www.gamify.ws/api/v1/events";
 
			URL obj = new URL(url);
			HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
	 		
			
			//add request header
			con.setRequestMethod("POST");
		
			con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
			con.setRequestProperty("charset", "utf-8");
			
			// Send post request
			con.setDoOutput(true);
			con.connect();
			DataOutputStream wr = new DataOutputStream(con.getOutputStream());
			wr.writeBytes(urlParameters);
			wr.flush();
			wr.close();
	 
			int responseCode = con.getResponseCode();
			System.out.println("\nSending 'POST' request to URL : " + url);
			System.out.println("Post parameters : " + urlParameters);
			System.out.println("Response Code : " + responseCode);
	 
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuffer response = new StringBuffer();
	 
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();
				 
			//print result
			System.out.println(response.toString());				
			} 
	
		// get the badges awarded by a specified user
		private void getUserBadges(String user_id) throws Exception {
			
			System.setProperty("jsse.enableSNIExtension", "false");

			JSONObject answerJson;
			String url = "https://www.gamify.ws/api/v1/badges_awarded/"+schemaName+"?user_id="+user_id;
	  	 
			URL obj = new URL(url);
			
			HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

			// optional default is GET
			con.setRequestMethod("GET");
	 
			//add request header
			con.setRequestProperty("User-Agent", USER_AGENT);

	 
			int responseCode1 = con.getResponseCode();

			System.out.println("\nSending 'GET' request to URL : " + url);

			System.out.println("Response Code : " + responseCode1);
			
	 
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));


			String inputLine;
			StringBuffer responseBuffer = new StringBuffer();
			
	 
			while ((inputLine = in.readLine()) != null) {
				responseBuffer.append(inputLine);
			}
			
			in.close();	
	 
			String response = responseBuffer.toString();
			//print result
		//	System.out.println(response.toString());
			// obtaining the badge's id 
			answerJson = new JSONObject(response);
 			
			JSONArray badgeArray = answerJson.getJSONArray("data");
			// now get the first element:
			JSONObject first = badgeArray.getJSONObject(0);
			String badge_id = first.getString("badge_id");		
			}
			
		
	   //compute the sha1 of a string
	   static String sha1(String input) throws NoSuchAlgorithmException {
	       MessageDigest mDigest = MessageDigest.getInstance("SHA1");
	       byte[] result = mDigest.digest(input.getBytes());
	       StringBuffer sb = new StringBuffer();
	       for (int i = 0; i < result.length; i++) {
	           sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
	       }
	        
	       return sb.toString();
	   		}

}
